package com.example.controller;

import com.example.common.Result;
import com.example.entity.Works;
import com.example.service.WorksService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：WorksController
 * 日期：2024/1/28 19:53
 */
@RestController
@RequestMapping("/works")
public class WorksController {

    @Resource
    private WorksService worksService;

    @PostMapping("/add")
    public Result add(@RequestBody Works works) {
        worksService.add(works);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody Works works) {
        worksService.updateById(works);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        worksService.deleteById(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        worksService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Works works = worksService.selectById(id);
        return Result.success(works);
    }

    //查询所有
    @GetMapping("/selectAll")
    public Result selectAll(Works works) {
        List<Works> worksList = worksService.selectAll(works);
        return Result.success(worksList);
    }
    //分页查询
    @GetMapping("/selectPage")
    public Result selectPage(Works works,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Works> pageInfo = worksService.selectPage(works, pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("selectHotWorks")
    public Result selectHotWorks(Works works,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "100") Integer pageSize) {
        PageInfo<Works> pageInfo = worksService.selectHotWorks(works, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
